3 namespace EZCameraShake
5 public enum CameraShakeState { FadingIn, FadingOut, Sustained, Inactive }
7 public class CameraShakeInstance
10 /// The intensity of the shake. It is recommended that you use ScaleMagnitude to alter the magnitude of a shake.
12 public float Magnitude
;
15 /// Roughness of the shake. It is recommended that you use ScaleRoughness to alter the roughness of a shake.
17 public float Roughness
;
20 /// How much influence this shake has over the local position axes of the camera.
22 public Vector3 PositionInfluence
;
25 /// How much influence this shake has over the local rotation axes of the camera.
27 public Vector3 RotationInfluence
;
30 /// Should this shake be removed from the CameraShakeInstance list when not active?
32 public bool DeleteOnInactive
= true;
35 float roughMod
= 1, magnMod
= 1;
36 float fadeOutDuration
, fadeInDuration
;
38 float currentFadeTime
;
43 /// Will create a new instance that will shake once and fade over the given number of seconds.
45 /// <param name="magnitude">The intensity of the shake.</param>
46 /// <param name="fadeOutTime">How long, in seconds, to fade out the shake.</param>
47 /// <param name="roughness">Roughness of the shake. Lower values are smoother, higher values are more jarring.</param>
48 public CameraShakeInstance(float magnitude
, float roughness
, float fadeInTime
, float fadeOutTime
)
50 this.Magnitude
= magnitude
;
51 fadeOutDuration
= fadeOutTime
;
52 fadeInDuration
= fadeInTime
;
53 this.Roughness
= roughness
;
65 tick
= Random
.Range(-100, 100);
69 /// Will create a new instance that will start a sustained shake.
71 /// <param name="magnitude">The intensity of the shake.</param>
72 /// <param name="roughness">Roughness of the shake. Lower values are smoother, higher values are more jarring.</param>
73 public CameraShakeInstance(float magnitude
, float roughness
)
75 this.Magnitude
= magnitude
;
76 this.Roughness
= roughness
;
79 tick
= Random
.Range(-100, 100);
82 public Vector3
UpdateShake()
84 amt
.x
= Mathf
.PerlinNoise(tick
, 0) - 0.5f
;
85 amt
.y
= Mathf
.PerlinNoise(0, tick
) - 0.5f
;
86 amt
.z
= Mathf
.PerlinNoise(tick
, tick
) - 0.5f
;
88 if (fadeInDuration
> 0 && sustain
)
90 if (currentFadeTime
< 1)
91 currentFadeTime
+= Time
.deltaTime
/ fadeInDuration
;
92 else if (fadeOutDuration
> 0)
97 currentFadeTime
-= Time
.deltaTime
/ fadeOutDuration
;
100 tick
+= Time
.deltaTime
* Roughness
* roughMod
;
102 tick
+= Time
.deltaTime
* Roughness
* roughMod
* currentFadeTime
;
104 return amt
* Magnitude
* magnMod
* currentFadeTime
;
108 /// Starts a fade out over the given number of seconds.
110 /// <param name="fadeOutTime">The duration, in seconds, of the fade out.</param>
111 public void StartFadeOut(float fadeOutTime
)
113 if (fadeOutTime
== 0)
116 fadeOutDuration
= fadeOutTime
;
122 /// Starts a fade in over the given number of seconds.
124 /// <param name="fadeInTime">The duration, in seconds, of the fade in.</param>
125 public void StartFadeIn(float fadeInTime
)
130 fadeInDuration
= fadeInTime
;
136 /// Scales this shake's roughness while preserving the initial Roughness.
138 public float ScaleRoughness
140 get { return roughMod; }
141 set { roughMod = value; }
145 /// Scales this shake's magnitude while preserving the initial Magnitude.
147 public float ScaleMagnitude
149 get { return magnMod; }
150 set { magnMod = value; }
154 /// A normalized value (about 0 to about 1) that represents the current level of intensity.
156 public float NormalizedFadeTime
157 { get { return currentFadeTime; }
}
160 { get { return currentFadeTime > 0 || sustain; }
}
163 { get { return !sustain && currentFadeTime > 0; }
}
166 { get { return currentFadeTime < 1 && sustain && fadeInDuration > 0; }
}
169 /// Gets the current state of the shake.
171 public CameraShakeState CurrentState
176 return CameraShakeState
.FadingIn
;
177 else if (IsFadingOut
)
178 return CameraShakeState
.FadingOut
;
180 return CameraShakeState
.Sustained
;
182 return CameraShakeState
.Inactive
;